/*++

Copyright (c) 2007  Microsoft Corporation 

Filename: 

    Options.cs

Abstract: 

    A form to take user input for the threshold values
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace NRTMAddIn
{
    public partial class Options : BaseForm
    {
        #region Variables
        //temp threshold variables
        private static string constRefThresholdValue = Global.GetResourceString("constRefThresholdValue_Options");         
        private string tempNormalVal_min = constRefThresholdValue;
        private string tempLowDiskVal_max = constRefThresholdValue;
        private string tempNormalVal_max = constRefThresholdValue;
        private string tempHealthyVal_min = constRefThresholdValue;
        #endregion

        #region Methods
        /// <summary>
        /// Constructor
        /// </summary>
        public Options()
        {
            InitializeComponent();
            this.gbThreshold.Text = Global.GetResourceString("Options_Threshold_Text");
            this.lbLowDisk.Text = Global.GetResourceString("DiskState_Low");
            this.lbNormal.Text = Global.GetResourceString("DiskState_Normal");
            this.lbHealthy.Text = Global.GetResourceString("DiskState_Healthy");
            this.lbState.Text = Global.GetResourceString("Options_State_Text");
            this.lbColor.Text = Global.GetResourceString("Options_Color_Text");
            this.lbMaxVal.Text = Global.GetResourceString("Options_Max_Text");
            this.lbMinVal.Text = Global.GetResourceString("Options_Min_Text");
            this.lbNote.Text = Global.GetResourceString("Asterisk");
            this.btnOk.Text = Global.GetResourceString("OKButton_Text");
            this.btnCancel.Text = Global.GetResourceString("CancelButton_Text");
            this.lbBlack.Text = Global.GetResourceString("Options_Unavail_Text");
            this.Text = Global.GetResourceString("Options_Form_Title");
            this.lowDiskVal_max.Text = Global.GetResourceString("Fifteen");
            this.lowDiskVal_min.Text = Global.GetResourceString("Zero");
            this.NormalVal_max.Text = Global.GetResourceString("Sixty");
            this.NormalVal_min.Text = Global.GetResourceString("Fifteen");
            this.HealthyVal_max.Text = Global.GetResourceString("Hundred");
            this.HealthyVal_min.Text = Global.GetResourceString("Sixty");
        }
                
        /// <summary>
        /// On click, Data validation of threshold values
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(NormalVal_min.Text) || string.IsNullOrEmpty(HealthyVal_min.Text))
                {
                    MessageBox.Show(Global.GetResourceString("Options_BlankTxtBox_Text"), 
                        Global.GetResourceString("NRTM_Options_MsgBox_Title"),
                        MessageBoxButtons.OK, MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button1,0
                        );
                    if (string.IsNullOrEmpty(HealthyVal_min.Text))
                    {
                        this.HealthyVal_min.Focus();
                    }
                    else
                    {
                        this.NormalVal_min.Focus();
                    }
                }
                else if (!(Convert.ToInt32(lowDiskVal_min.Text, CultureInfo.InvariantCulture) >= 0 && Convert.ToInt32(lowDiskVal_max.Text, CultureInfo.InvariantCulture) < 100
                    && Convert.ToInt32(NormalVal_min.Text, CultureInfo.InvariantCulture) > 0 && Convert.ToInt32(NormalVal_max.Text, CultureInfo.InvariantCulture) < 100
                    && Convert.ToInt32(HealthyVal_min.Text, CultureInfo.InvariantCulture) > 0 && Convert.ToInt32(HealthyVal_max.Text, CultureInfo.InvariantCulture) <= 100))
                {
                    MessageBox.Show(Global.GetResourceString("Options_ThreshVal_Text"), 
                        Global.GetResourceString("NRTM_Options_MsgBox_Title"),
                        MessageBoxButtons.OK, MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button1);
                }
                else if (Convert.ToInt32(NormalVal_min.Text, CultureInfo.InvariantCulture) > Convert.ToInt32(NormalVal_max.Text, CultureInfo.InvariantCulture))
                {
                    MessageBox.Show(Global.GetResourceString("Options_ExceedVal_Text"), 
                        Global.GetResourceString("NRTM_Options_MsgBox_Title"),
                        MessageBoxButtons.OK, MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button1);
                    this.NormalVal_min.Focus();
                    return;
                }                
                else
                {
                    Global.MinValRed = Int32.Parse(lowDiskVal_min.Text, CultureInfo.InvariantCulture);
                    Global.MaxValRed = Int32.Parse(lowDiskVal_max.Text, CultureInfo.InvariantCulture);
                    Global.MinValYellow = Int32.Parse(NormalVal_min.Text, CultureInfo.InvariantCulture);
                    Global.MaxValYellow = Int32.Parse(NormalVal_max.Text, CultureInfo.InvariantCulture);
                    Global.MinValGreen = Int32.Parse(HealthyVal_min.Text, CultureInfo.InvariantCulture);
                    Global.MaxValGreen = Int32.Parse(HealthyVal_max.Text, CultureInfo.InvariantCulture);

                    this.DialogResult = DialogResult.Cancel;
                    return;
                }
            }
            catch (NrtmException ex)
            {
                //To catch and display exceptions from sub functions
                NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex.InnerException);
            }
            catch (Exception ex)
            {
                //To catch exceptions from current functions
                NrtmException.HandleExceptions(Global.GetResourceString("Exception_MsgBox_Caption"),
                            Global.GetResourceString("Exception_UsrMsg"), ex);
            }
        }

        /// <summary>
        /// On Cancel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            return;
        }

        /// <summary>
        /// Check Numeric Values
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        private static bool IsNumeric(string val)
        {
            try
            {
                if (string.IsNullOrEmpty(val))
                {
                    return true;
                }
                else if (val == "-")
                {
                    return false;
                }
                else
                {
                    Convert.ToInt32(val, CultureInfo.InvariantCulture);
                    return true;
                }
            }
            catch
            {
                MessageBox.Show(Global.GetResourceString("Options_NonNumeric_Text"),
                    Global.GetResourceString("NRTM_Options_MsgBox_Title"),
                    MessageBoxButtons.OK, MessageBoxIcon.Error,
                        MessageBoxDefaultButton.Button1);
                return false;
            }
        }
        #endregion         

        #region Events
        /// <summary>
        /// On Max Healthy Value changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HealthyVal_max_TextChanged(object sender, EventArgs e)
        {
            HealthyVal_max.Text = Global.GetResourceString("Hundred");
        }

        /// <summary>
        /// On Min Healthy Value Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HealthyVal_min_TextChanged(object sender, EventArgs e)
        {
            if (IsNumeric(HealthyVal_min.Text))
            {
                tempHealthyVal_min = HealthyVal_min.Text;

                if (string.IsNullOrEmpty(tempHealthyVal_min))
                {
                    tempHealthyVal_min = string.Empty;
                    NormalVal_max.Text = tempHealthyVal_min;
                    tempNormalVal_max = constRefThresholdValue;
                    tempHealthyVal_min = constRefThresholdValue;
                }
                else if (tempNormalVal_max != NormalVal_max.Text)
                {
                    NormalVal_max.Text = Convert.ToString(Int32.Parse(tempHealthyVal_min, CultureInfo.InvariantCulture) - 1, CultureInfo.InvariantCulture);
                }
                else
                {
                    tempNormalVal_max = constRefThresholdValue;
                    tempHealthyVal_min = constRefThresholdValue;
                }
            }
            else
            {
                if (HealthyVal_min.TextLength == 1)
                {
                    HealthyVal_min.Text = string.Empty;
                }
                else
                {
                    HealthyVal_min.Text = Convert.ToString(Int32.Parse(NormalVal_max.Text, CultureInfo.InvariantCulture) + 1, CultureInfo.InvariantCulture);
                }
            }
        }

        /// <summary>
        /// On Min Normal Value changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NormalVal_min_TextChanged(object sender, EventArgs e)
        {
            if (IsNumeric(NormalVal_min.Text))
            {
                tempNormalVal_min = NormalVal_min.Text;

                if (string.IsNullOrEmpty(tempNormalVal_min))
                {
                    tempNormalVal_min = string.Empty;
                    lowDiskVal_max.Text = tempNormalVal_min;
                    tempNormalVal_min = constRefThresholdValue;
                    tempLowDiskVal_max = constRefThresholdValue;
                }
                else if (tempLowDiskVal_max != lowDiskVal_max.Text)
                {
                    lowDiskVal_max.Text = Convert.ToString(Int32.Parse(NormalVal_min.Text, CultureInfo.InvariantCulture) - 1, CultureInfo.InvariantCulture);
                }
                else
                {
                    tempNormalVal_min = constRefThresholdValue;
                    tempLowDiskVal_max = constRefThresholdValue;
                }
            }
            else
            {
                if (NormalVal_min.TextLength == 1)
                {
                    NormalVal_min.Text = string.Empty;
                }
                else
                {
                    NormalVal_min.Text = Convert.ToString(Int32.Parse(lowDiskVal_max.Text, CultureInfo.InvariantCulture) + 1, CultureInfo.InvariantCulture);
                }
            }
        }

        /// <summary>
        /// On Max Normal Value changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NormalVal_max_TextChanged(object sender, EventArgs e)
        {
            if (IsNumeric(NormalVal_max.Text))
            {
                tempNormalVal_max = NormalVal_max.Text;

                if (string.IsNullOrEmpty(tempNormalVal_max))
                {
                    tempNormalVal_max = string.Empty;
                    HealthyVal_min.Text = tempNormalVal_max;
                    tempNormalVal_max = constRefThresholdValue;
                    tempHealthyVal_min = constRefThresholdValue;
                }
                else if (tempHealthyVal_min != HealthyVal_min.Text)
                {
                    HealthyVal_min.Text = Convert.ToString(Int32.Parse(tempNormalVal_max, CultureInfo.InvariantCulture) + 1, CultureInfo.InvariantCulture);
                }
                else
                {
                    tempNormalVal_max = constRefThresholdValue;
                    tempHealthyVal_min = constRefThresholdValue;
                }
            }
            else
            {
                if (NormalVal_max.TextLength == 1)
                {
                    NormalVal_max.Text = string.Empty;
                }
                else
                {
                    NormalVal_max.Text = Convert.ToString(Int32.Parse(HealthyVal_min.Text, CultureInfo.InvariantCulture) - 1, CultureInfo.InvariantCulture);
                }
            }
        }

        /// <summary>
        /// On Min Low Disk Value Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lowDiskVal_min_TextChanged(object sender, EventArgs e)
        {
            lowDiskVal_min.Text = Global.GetResourceString("Zero");
        }

        /// <summary>
        /// On Max Low Disk Value Changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lowDiskVal_max_TextChanged(object sender, EventArgs e)
        {

            if (IsNumeric(lowDiskVal_max.Text))
            {
                tempLowDiskVal_max = lowDiskVal_max.Text;

                if (string.IsNullOrEmpty(tempLowDiskVal_max))
                {
                    tempLowDiskVal_max = string.Empty;
                    NormalVal_min.Text = tempLowDiskVal_max;
                    tempNormalVal_min = constRefThresholdValue;
                    tempLowDiskVal_max = constRefThresholdValue;
                }
                else if (tempNormalVal_min != NormalVal_min.Text)
                {
                    NormalVal_min.Text = Convert.ToString(Int32.Parse(lowDiskVal_max.Text, CultureInfo.InvariantCulture) + 1, CultureInfo.InvariantCulture);
                }
                else
                {
                    tempNormalVal_min = constRefThresholdValue;
                    tempLowDiskVal_max = constRefThresholdValue;
                }
            }
            else
            {
                if (lowDiskVal_max.TextLength == 1)
                {
                    lowDiskVal_max.Text = string.Empty;
                }
                else
                {
                    lowDiskVal_max.Text = Convert.ToString(Int32.Parse(NormalVal_min.Text, CultureInfo.InvariantCulture) - 1, CultureInfo.InvariantCulture);
                }
            }
        }

        /// <summary>
        /// On Form Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Options_Load(object sender, EventArgs e)
        {
            lowDiskVal_min.Text = Convert.ToString(Global.MinValRed, CultureInfo.InvariantCulture);
            lowDiskVal_max.Text = Convert.ToString(Global.MaxValRed, CultureInfo.InvariantCulture);
            NormalVal_min.Text = Convert.ToString(Global.MinValYellow, CultureInfo.InvariantCulture);
            NormalVal_max.Text = Convert.ToString(Global.MaxValYellow, CultureInfo.InvariantCulture);
            HealthyVal_min.Text = Convert.ToString(Global.MinValGreen, CultureInfo.InvariantCulture);
            HealthyVal_max.Text = Convert.ToString(Global.MaxValGreen, CultureInfo.InvariantCulture);
        }
   
        #endregion

     }
}
